package com.bw.spsy.pay.order;

import com.bwie.poi.StringUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author: liuzhiping
 * @date: 2025/5/24 11:20
 **/
public class CommUtil2210a {


    /****
     *
     * @param data Map{ "a":1  "c"：3  "b"：2 "sign":"WERHWEKRW083EEW"}     【a=1&b=2&c=3】
     * @param key
     * @param signType
     * @return
     */
    public static String generateSignature(final Map<String, String> data, String key, String signType) throws Exception{
        //获取map中的key{"a","c","b"}
        Set<String> setKey =  data.keySet();

        //把key放到数组里面 {"a","c","b"}[3]
        String[] setKeys = setKey.toArray(new String[setKey.size()]);

        //排序key{"a","b","c"}
        Arrays.sort(setKeys);

        StringBuffer buffer = new StringBuffer();

     /*   for(Map.Entry entry :data.entrySet()){
            buffer.append(k).append("=").append(value).append("&");
        }*/
        //setKeys {"a","b","c"}  轮询3次
        for(String k : setKeys){ //第一次 k=a  第二次 k=b 第三次 k=c
            String value = data.get(k);//第一次 value=1 value=2 value=3

            if(k.toUpperCase().equals("SIGN")){
                continue;
            }
            if(StringUtils.isEmpty(value)){
                continue;
            }
            //第一次 a=1&  ；第二次 a=1&b=2& ；第三次 a=1&b=2&c=3&
            buffer.append(k).append("=").append(value).append("&");
        }
        System.out.println("--------->111buffer:"+buffer.toString());//a=1&b=2&c=3&

        buffer.append("key=").append(key);

        System.out.println("--------->2222buffer:"+buffer.toString());//a=1&b=2&c=3&

        if(signType.toUpperCase().equals("MD5")){
            return CommUtil.MD5(buffer.toString()).toUpperCase();
        }else if(signType.toUpperCase().equals("HMACSHA256")){
            return HMACSHA256(buffer.toString(), key);
        }
        throw  new Exception("没有这个算法");

    }


    public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }


    /**
     * 生成 MD5
     *
     * @param data
     *            待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }


    public static void main(String[] args)  throws Exception{
        Map<String,String> data = new HashMap<>();
        data.put("a","1");
        data.put("c","3");
        data.put("b","2");
        String zjSignstr = CommUtil2210a.generateSignature(data, Constants.API_KEY,"HMACSHA256");

        System.out.println("zjSignstr="+zjSignstr);

        String wxSignStr = CommUtil.generateSignature(data, Constants.API_KEY,"HMACSHA256");

        System.out.println("wxSignStr="+wxSignStr);

        //System.out.println(str);
    }
}
